<template>
  <ComponentContainerProperty v-model="formData.style">
    <el-form label-width="80px" :model="formData" :rules="rules">
      <el-form-item label="选择公告">
        <el-input v-model="noticeTitle" placeholder="请选择公告" readonly style="width: 240px; margin-right: 8px;" />
        <el-button @click="openNoticeSelect">选择公告</el-button>
      </el-form-item>
  
      <el-form-item label="公告图标" prop="iconUrl">
        <UploadImg v-model="formData.iconUrl" height="48px">
          <template #tip>建议尺寸：24 * 24</template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="更多图标" prop="arrowUrl">
        <UploadImg v-model="formData.arrowUrl" height="48px">
          <template #tip>建议尺寸：24 * 24</template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="更多链接">
        <AppLinkInput v-model="formData.noticeMoreUrl" placeholder="请输入更多跳转链接" />
      </el-form-item>
      <el-form-item label="背景颜色" prop="backgroundColor">
        <ColorInput v-model="formData.backgroundColor" />
      </el-form-item>
      <el-form-item label="文字颜色" prop="文字颜色">
        <ColorInput v-model="formData.textColor" />
      </el-form-item>
    </el-form>
    <NoticeTableSelect ref="noticeTableSelectRef" @change="handleNoticeSelected" />
  </ComponentContainerProperty>
</template>

<script setup lang="ts">
import { NoticeBarProperty } from './config'
import { usePropertyForm } from '@/components/DiyEditor/util'
import NoticeTableSelect from './NoticeTableSelect.vue'
import { getNotice } from '@/api/system/notice'

// 通知栏属性面板
defineOptions({ name: 'NoticeBarProperty' })
// 表单校验
const rules = {
}

const props = defineProps<{ modelValue: NoticeBarProperty }>()
const emit = defineEmits(['update:modelValue'])
const { formData } = usePropertyForm(props.modelValue, emit)

const noticeTitle = ref('')
const noticeTableSelectRef = ref()

const openNoticeSelect = () => {
  noticeTableSelectRef.value.open()
}

const handleNoticeSelected = async (notice) => {
  formData.value.noticeId = notice.id
  noticeTitle.value = notice.title
}

watch(
  () => formData.value.noticeId,
  async (id) => {
    if (id) {
      const { title } = await getNotice(id)
      noticeTitle.value = title
    } else {
      noticeTitle.value = ''
    }
  },
  { immediate: true }
)
</script>

<style scoped lang="scss"></style>
